// pages/exchange/exchange.js
Page({
  data: {
    sortType: 'hot', // 当前选中项，只允许 'hot' 或 'new'
    canExchange: false,
    contentHeight: 0,
    avatarUrl: 'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/头像.jpg',
    points: [
      { name: '星币', value: 0 },
      { name: '星票', value: 0 },
      { name: '娃娃点', value: 0 }
    ],
    cart: {
      cartCount: 0, // 保存购物车商品数量
      emptyCartIcon: 'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/exchage/购物车空.png',
      fullCartIcon: 'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/exchage/购物车满.png'
    },
    currentIndex: 0,
    categories: [
      { name: '全部', },
      { name: '精美礼品' },
      { name: '日用百货' }
    ],
    goods: [
      { id: '精美水杯', title: '双层玻璃防摔，保温保冷', price: '4' ,image:'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/exchage/水杯.jpg'},
      { id: '电风扇', title: '低泡易漂，温和去污', price: '4' ,image:'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/exchage/雨伞.png'},
    ],
  },
  goToCart() {
    wx.navigateTo({
      url: '/pages/exchange/Cart/cart',
    })
  },

  goToDetail(e) {
    const { id, title, price } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/exchange/goods-detail/goods-detail?id=${id}&title=${encodeURIComponent(title)}&price=${price}`
    });
  },
  onSortTap(e) {
    const type = e.currentTarget.dataset.type;

    if (type === 'hot' || type === 'new') {
      this.setData({
        sortType: type
      });
    }
  },
  onCanExchangeToggle() {
    this.setData({
      canExchange: !this.data.canExchange
    });
  },
  onMenuClick(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({ currentIndex: index });
  },
  goTostoreRecords() {
    wx.navigateTo({
      url: '/pages/storedRecords/storedRecords',
    })
  },
  goToRecord() {
    wx.navigateTo({
      url: '/pages/record/record'
    });
  },
  onShow() {
    this.updateCartIcon(); // 每次页面显示时更新
    // console.log(this.data.cart.cartCount);
  },
  updateCartIcon() {
    const cart = wx.getStorageSync('cart');
    console.log(cart.length);
    const count = Array.isArray(cart) ? cart.length : 0;

    this.setData({
      cart: {
        ...this.data.cart,
        cartCount: count
      }
    });
  },
}); 